<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Area;
use app\common\model\Menu;
use app\common\model\Role;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\View;
use think\Response;

class Index extends Base
{
    protected array $sort = [];

    public function index(): string
    {
        $rules = '';
        if ($this->admin->role_id != 1) {
            $rules = (new Role())->where('id', $this->admin->role_id)->value('rules');
        }
        View::assign([
            'uni_path' => !empty($this->system['uni_path']) ? $this->system['uni_path'] : '/',
            'menus' => Menu::getNewMenu(1, $rules),
            'attachMenu' => $this->admin->role_id == 1 ? getAttachMenu() : []
        ]);

        return View::fetch();
    }

    public function welcome(): string
    {
        return View::fetch();
    }

    public function clearCache(): Response
    {
        deldir('download/');

        return Cache::clear() ? Result::success('清除成功') : Result::fail('加载失败');
    }

    public function getRemind(): Response
    {
        $tips = Cache::get('tips');
        if (!empty($tips)) {
            Cache::delete('tips');

            return Result::success('success', $tips);
        } else {

            return Result::fail('暂无消息提醒');
        }
    }

    public function getArea($id = 0): Response
    {
        return Result::success('success', Area::getArea($id));
    }
}